$(document).ready(function(){
	function check(name,reg){
		var name_value = $("#"+name).val();
		var flag;
		flag = reg.test(name_value);
		return flag;
	}
	function checkRepassword(){
		var newpassword = $("#newpassword").val();
		var newpassword2 = $("#newpassword2").val();
		if(newpassword!==newpassword2 || newpassword2.length == 0){
			return false;
		}else{
			return true;
		}
	}
	//ajax post submit请求
	$(".ajax-post").click(function(){
		var target,query,form;
		var target_form = $(this).attr('target-form');
		var that = this;
		var newpassword,checkpassword;
		newpassword_flag = check("newpassword",/^[a-zA-Z]\w{5,17}$/);
		if(!newpassword_flag){
			$(".error_prompt").html("以字母开头，长度在6~18之间，只能包含字符、数字和下划线");
			return false;
		}
		checkpassword_flag = checkRepassword();
		if(!checkpassword_flag){
			$(".error_prompt").html("未输入或两次输入密码不同");
			return false;
		}
		form = $('.'+target_form);
		query = form.serialize();
		//console.log(query);
		target = form.get(0).action;
		$.post(target,query,function(data){
			if(data.flag == 0){
    			alert(data.content);
    		}else if(data.flag == 1){
    			updateAlert(data.content,true);
    			setTimeout(function(){
					if(data.url)
					{
						location.href=data.url;
					}
					else{
						location.reload();
					}
				},1500);
    		}else if(data.flag ==-1){
    			updateAlert(data.content);
    		}
		},"json");
	});
	
	
	//警告栏显示
	var am_alert = $('#top-alert');
	var alert_button = am_alert.find('button');
	alert_button.click(function(){
		am_alert.hide();
	});
	window.updateAlert = function(text,c)
	{
		text = text||'default';
		c = c||false;
		am_alert.css("display","block");
		if(text!='default'){
			am_alert.find('p').text(text);
		}
		else{
			am_alert.find('p').text('这是内容!');
		}
		if(c==false)
		{
			am_alert.removeClass("alert-success").addClass(' alert-error');
		}else{
			am_alert.removeClass("alert-error").addClass(" alert-success");
		}
	}
});